/*
 * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#include "sdk.h"
#include "securec.h"

/* -------------------------------------------------------------------------- */
/* --- PRIVATE DEPENDANCIES ------------------------------------------------- */
#include "trace.h"
#include "sample_lcd.h"
#include "ffmpeg_rtmp.h"
#include "svp_nnie_application.h"
#include "main_process.h"

/* -------------------------------------------------------------------------- */
/* --- PRIVATE VARIABLES ---------------------------------------------------- */
HI_BOOL stop_flag = HI_FALSE;
HI_BOOL exit_flag = HI_FALSE;

/* -------------------------------------------------------------------------- */
/* --- PRIVATE FUNCTIONS ---------------------------------------------------- */
/* function : show usage */
static void SAMPLE_SVP_Usage(const char *pchPrgName)
{
    printf("Usage : %s <index> \n", pchPrgName);
    printf("index:\n");
    printf("\t 0) Detection for picture to test.\n");
    printf("\t 1) Detection for video to test, frame from sensor cam.\n");
    printf("\t 2) Goto the main process.\n");
}

static void SAMPLE_SVP_HandleSig(int s32Signo)
{
    if (s32Signo == SIGINT || s32Signo == SIGTERM) {
        SVP_NNIE_Yolov5_HandleSig();
        Application_HandleSig();
        MSG_DEBUG(DEBUG_LOG, "[INFO] MAIN: You have pressed Ctrl+C to stop process.\n");
    }
}

/* function : nnie sample */
int main(int argc, char *argv[])
{
    HI_S32 s32Ret = HI_SUCCESS;
    sdk_init();

    /* Stop and exit process */
    struct sigaction sa;
    (hi_void)memset_s(&sa, sizeof(struct sigaction), 0, sizeof(struct sigaction));
    sa.sa_handler = SAMPLE_SVP_HandleSig;
    sa.sa_flags = 0;
    sigaction(SIGINT, &sa, NULL);
    sigaction(SIGTERM, &sa, NULL);

    if (argc < 2 || argc > 2) { /* only support 2 parameters */
        SAMPLE_SVP_Usage(argv[0]);
        s32Ret = HI_FAILURE;
        goto end;
    }

    if (strncmp(argv[1], "-h", 2) == 0) { /* 2: maximum number of characters to compare */
        SAMPLE_SVP_Usage(argv[0]);
        s32Ret = HI_SUCCESS;
        goto end;
    }

    switch (*argv[1]) {
        case '0':
            MSG_DEBUG(DEBUG_LOG, "[INFO] : [MAIN] detection for single picture.\n");
            SVP_NNIE_Yolov5_Lite();
            break;
        case '1':
            MSG_DEBUG(DEBUG_LOG, "[INFO] : [MAIN] test sensor input and mipi output.\n");
            /*
             * MIPI为GPIO55，开启液晶屏背光
             * MIPI is GPIO55, Turn on the backlight of the LCD screen
             */
            system("cd /sys/class/gpio/;echo 55 > export;echo out > gpio55/direction;echo 1 > gpio55/value");
            s32Ret = SampleVioVpssVoMipi();
            break;
        case '2':
            MSG_DEBUG(DEBUG_LOG, "[INFO] : [MAIN] goto the main process.\n");
            SampleYolov5ViVpssVencVo();
            break;
        default:
            break;
    }

end:
    MSG_DEBUG(DEBUG_LOG, "[INFO] : [MAIN] exit sdk process.\n");
    sdk_exit();
    return s32Ret;
}
